<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getOverviewApi } from '@/api/home'
import { ElMessage } from 'element-plus'
import { Document, Briefcase, User,  Notebook, Calendar,  Memo} from '@element-plus/icons-vue'

const router = useRouter()

// 概览数据
const overviewData = ref({
  clueTotal: 0,
  clueWaitAllot: 0,
  clueWaitFollow: 0,
  clueFollowing: 0,
  clueFalse: 0,
  clueConvertBusiness: 0,
  businessTotal: 0,
  businessWaitAllot: 0,
  businessWaitFollow: 0,
  businessFollowing: 0,
  businessFalse: 0,
  businessConvertCustomer: 0
})

// 快捷操作菜单
const quickActions = [
  {
    title: '课程管理',
    subtitle: '课程信息维护',
    icon: Notebook,
    route: '/resource/course',
    color: '#409EFF'
  },
  {
    title: '活动管理',
    subtitle: '营销活动管理',
    icon: Calendar,
    route: '/resource/activity',
    color: '#67C23A'
  },
  {
    title: '线索管理',
    subtitle: '潜在客户追踪',
    icon: Document,
    route: '/clue/list',
    color: '#E6A23C'
  },
  {
    title: '商机管理',
    subtitle: '销售机会管理',
    icon: Briefcase,
    route: '/business/list',
    color: '#F56C6C'
  },
  {
    title: '客户管理',
    subtitle: '客户信息维护',
    icon: User,
    route: '/customer',
    color: '#909399'
  },
  {
    title: '日志统计',
    subtitle: '系统操作日志',
    icon: Memo,
    route: '/system/log',
    color: '#9C27B0'
  }
]

onMounted(() => {
  loadOverviewData()
})

// 加载概览数据
const loadOverviewData = async () => {
  try {
    const result = await getOverviewApi()
    if (result.code === 1) {
      overviewData.value = result.data
    } else {
      ElMessage.error(result.msg || '获取概览数据失败')
    }
  } catch (error) {
    console.error('获取概览数据失败:', error)
    ElMessage.error('获取概览数据失败，请稍后重试')
  }
}

// 快捷操作跳转
const handleQuickAction = (route) => {
  router.push(route)
}
</script>

<template>
  <div class="home-container">
    <!-- 欢迎标题 -->
    <div class="welcome-section">
      <h1 class="welcome-title">欢迎您，进入轻客管家系统</h1>
    </div>

    <!-- 概览数据区域 -->
    <div class="overview-section">
      <!-- 线索概览 -->
      <div class="overview-card">
        <div class="card-header">
          <h3 class="card-title">线索概览</h3>
        </div>
        <div class="card-content">
          <div class="metric-grid">
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueTotal }}</div>
              <div class="metric-label">总线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueWaitAllot }}</div>
              <div class="metric-label">待分配线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueWaitFollow }}</div>
              <div class="metric-label">待跟进线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueFollowing }}</div>
              <div class="metric-label">跟进中线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueFalse }}</div>
              <div class="metric-label">伪线索数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.clueConvertBusiness }}</div>
              <div class="metric-label">转商机线索数</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 商机概览 -->
      <div class="overview-card">
        <div class="card-header">
          <h3 class="card-title">商机概览</h3>
        </div>
        <div class="card-content">
          <div class="metric-grid">
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessTotal }}</div>
              <div class="metric-label">总商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessWaitAllot }}</div>
              <div class="metric-label">待分配商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessWaitFollow }}</div>
              <div class="metric-label">待跟进商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessFollowing }}</div>
              <div class="metric-label">跟进中商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessFalse }}</div>
              <div class="metric-label">回收商机数</div>
            </div>
            <div class="metric-item">
              <div class="metric-value">{{ overviewData.businessConvertCustomer }}</div>
              <div class="metric-label">转客户商机数</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作区域 -->
    <div class="quick-actions-section">
      <h3 class="section-title">快捷操作</h3>
      <div class="actions-grid">
        <div 
          v-for="action in quickActions" 
          :key="action.title"
          class="action-card"
          @click="handleQuickAction(action.route)"
        >
          <div class="action-icon" :style="{ backgroundColor: action.color }">
            <el-icon :size="24" color="#fff">
              <component :is="action.icon" />
            </el-icon>
          </div>
          <div class="action-content">
            <div class="action-title">{{ action.title }}</div>
            <div class="action-subtitle">{{ action.subtitle }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-container {
  padding: 20px;
  background-color: #fff;
  min-height: 100%;
  border-radius: 8px;
}

.welcome-section {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-title {
  font-size: 28px;
  color: #333;
  margin: 0;
  font-weight: 500;
}

.overview-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 30px;
}

.overview-card {
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.card-header {
  margin-bottom: 20px;
}

.card-title {
  font-size: 18px;
  color: #333;
  margin: 0;
  font-weight: 500;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
}

.metric-item {
  text-align: center;
  padding: 15px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease;
}

.metric-item:hover {
  transform: translateY(-2px);
}

.metric-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 8px;
}

.metric-label {
  font-size: 12px;
  color: #666;
  line-height: 1.4;
}

.quick-actions-section {
  margin-top: 30px;
}

.section-title {
  font-size: 18px;
  color: #333;
  margin-bottom: 20px;
  font-weight: 500;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 20px;
}

.action-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;
}

.action-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  border-color: #409EFF;
}

.action-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 15px;
}

.action-content {
  text-align: center;
}

.action-title {
  font-size: 16px;
  color: #333;
  font-weight: 500;
  margin-bottom: 8px;
}

.action-subtitle {
  font-size: 12px;
  color: #666;
  line-height: 1.4;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .actions-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .overview-section {
    grid-template-columns: 1fr;
  }
  
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
